<template>
   
  <div>
  
    <div class="table-container">
      <el-table
        ref="productRelationTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
      <el-table-column label="商品名称"  align="center">
          <template slot-scope="scope"><img class="imgs" :src="scope.row.coverImgUrl" style="width: 100px; height: 100px"  /></template>
        </el-table-column>
        <el-table-column label="商品名称"  align="center">
          <template slot-scope="scope">{{ scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="价格" align="center">
          <template slot-scope="scope">{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column label="链接"  align="center">
          <template slot-scope="scope"
            >{{ scope.row.url}}</template
          >
        </el-table-column>

        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">

            <el-button v-if="status!=1"
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
            <div v-else style="color:#dcdcdc">审核商品不能操作</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 20, 30]"
        :total="total"
      >
      </el-pagination>
    </div>
    
  
  </div>
</template>
<script>
import {
  fetchList,
  createFlashProductRelation,
  deleteFlashProductRelation,
  updateFlashProductRelation,
} from "@/api/flashProductRelation";
import { addImg, addProduct,productList ,productDel} from "@/api/wx";
import { fetchList as fetchProductList } from "@/api/product";
import { fetchList as fetchProductSkuList } from "@/api/skuStock";
import axios from "axios";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  flashPromotionId: null,
  flashPromotionSessionId: null,
};

export default {
  name: "flashPromotionProductRelationList",
  data() {
   const validatePass = (rule, value, callback) => {
     console.log(value)
        if (value === '') {
          callback(new Error('请输入商品名称'));
        }
        let len = 0;
        for (var i = 0; i < this.dialogFormVisibleData.name.length; i++) {
          let c = this.dialogFormVisibleData.name.charCodeAt(i);
          //单字节加1
          if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            len += 0.5;
          } else {
            len += 1;
          }
        }
        if (len < 3 || len > 13) {
          callback(new Error('商品名称必须为3-14个汉字（一个字等于两个英文字符或特殊字符）'));

        }else{
 callback();
        }
      };
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      dialogVisible: false,
      selectDialogVisible: false,
      dialogFormVisible: false,
      dialogFormVisibleData: {},
      dialogFormVisibleDataNameBoo:false,
       rules: {
          name: [
            { validator: validatePass, trigger: 'blur' }
          ]
        },
      dialogData: {
        list: null,
        total: null,
        multipleSelection: [],
        listQuery: {
          keyword: null,
          pageNum: 1,
          pageSize: 10,
        },
      },
      editDialogVisible: false,
      flashProductRelation: {
        product: {},
      },
      aa: null,
      brandOptions: [],
    };
  },
  props:{
    status:{
      type:Number,
      default:2
    }
  },
  created() {
    this.listQuery.flashPromotionId = this.$route.query.flashPromotionId;
    this.listQuery.flashPromotionSessionId = this.$route.query.flashPromotionSessionId;
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    
    handleWithdraw(index, row) {
      
    },
    handleDelete(index, row) {
      console.log(row)
      this.$confirm("是否要删除该商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        productDel(row.goodsId).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      });
    },
    handleSelectSearch() {
      this.getDialogList();
    },
    
  
  
    

    
    getList() {
      console.log(this.status,this.listQuery.pageNum)
      this.listLoading = true;
      productList(((this.listQuery.pageNum-1)*this.listQuery.pageSize),this.listQuery.pageSize,this.status).then((response) => {
        console.log()
        this.listLoading = false;
        this.list = JSON.parse(response.data).goods;
        this.total = JSON.parse(response.data).total;

      });
     
     },
    getDialogList() {
      fetchProductList(this.dialogData.listQuery).then((response) => {
        this.dialogData.list = response.data.list;
        this.dialogData.total = response.data.total;
      });
    },
    handleBrandChange(val) {
      for (let i = 0; i < this.brandOptions.length; i++) {
        if (this.brandOptions[i].id === val) {
          this.flashProductRelation.productSkuStockId = this.brandOptions[i].id;
          break;
        }
      }
    },
  },
  filters: {
    spData(data) {
      let name = "";
      if (data != null && data != "") {
        JSON.parse(data).map((e) => {
          name += e.key + ":" + e.value;
        });
      }

      return name;
    },
  },
};
</script>
<style scoped>
.operate-container {
  margin-top: 0;
}
.input-width {
  width: 200px;
}
</style>


